

SRC = ./src
BINS = ./bin
INCLUDE = ./headers
LIBS = ./libs

GTEST_DIR = $(LIBS)/gtest-1.6.0

# Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR)/include -I$(INCLUDE)

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra

ALL_TESTS = $(BINS)/regex_prefix_matcher_unittest $(BINS)/lexer_unittest

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

# House-keeping build targets.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

# make repl - build interactive calculator
repl: $(SRC)/lexer.o $(SRC)/parser.o $(SRC)/regex_prefix_matcher.o $(SRC)/repl.o $(SRC)/result.o $(SRC)/token.o
	@echo "Linking REPL..."
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $^ -o $(BINS)/repl

$(SRC)/%.o: $(SRC)/%.cpp
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $^ -o $@

# make tests - build and run all tests
tests: $(ALL_TESTS)
	$(BINS)/lexer_unittest
	$(BINS)/regex_prefix_matcher_unittest

# make regexp-lexer-test - build and run Lexer tests
lexer-test: $(BINS)/lexer_unittest
	@echo "Starting lexer tests..."
	$(BINS)/lexer_unittest

# make regexp-matcher-test - build and run Regexp matcher tests
regexp-matcher-test: $(BINS)/regex_prefix_matcher_unittest
	@echo "Starting regexp matcher tests..."
	$(BINS)/regex_prefix_matcher_unittest

# make help - get help
help:
	@grep "^# make" ./Makefile 

clean :
	rm -f $(LIBS)/gtest.a $(LIBS)/gtest_main.a $(SRC)/*.o $(BINS)/*

# Builds Google Test library

$(GTEST_DIR)/gtest-all.o : $(GTEST_SRCS_)
	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc -o $(GTEST_DIR)/gtest-all.o

$(GTEST_DIR)/gtest_main.o : $(GTEST_SRCS_)
	$(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc -o $(GTEST_DIR)/gtest_main.o

$(GTEST_DIR)/gtest.a : $(GTEST_DIR)/gtest-all.o
	$(AR) $(ARFLAGS) $@ $^

$(GTEST_DIR)/gtest_main.a : $(GTEST_DIR)/gtest-all.o $(GTEST_DIR)/gtest_main.o
	$(AR) $(ARFLAGS) $@ $^

# Builds regexp matcher tests

$(SRC)/regex_prefix_matcher.o : $(SRC)/regex_prefix_matcher.cpp $(INCLUDE)/regex_prefix_matcher.hpp $(GTEST_HEADERS) 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(SRC)/regex_prefix_matcher.cpp -o $(SRC)/regex_prefix_matcher.o

$(SRC)/regex_prefix_matcher_unittest.o : $(SRC)/regex_prefix_matcher_unittest.cpp \
            $(INCLUDE)/regex_prefix_matcher.hpp $(GTEST_HEADERS)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(SRC)/regex_prefix_matcher_unittest.cpp -o $(SRC)/regex_prefix_matcher_unittest.o

$(BINS)/regex_prefix_matcher_unittest : $(SRC)/regex_prefix_matcher.o $(SRC)/regex_prefix_matcher_unittest.o $(GTEST_DIR)/gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -pthread $^ -o $@

# Builds lexer tests

$(SRC)/lexer.o : $(SRC)/lexer.cpp $(INCLUDE)/lexer.hpp $(GTEST_HEADERS) 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(SRC)/lexer.cpp -o $(SRC)/lexer.o

$(SRC)/lexer_unittest.o : $(SRC)/lexer_unittest.cpp  \
            $(INCLUDE)/lexer.hpp $(GTEST_HEADERS) 
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(SRC)/lexer_unittest.cpp -o $(SRC)/lexer_unittest.o

$(BINS)/lexer_unittest : $(SRC)/lexer.o $(SRC)/lexer_unittest.o $(SRC)/regex_prefix_matcher.o $(GTEST_DIR)/gtest_main.a
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -pthread $^ -o $@


